<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<title>数据表字段管理页面</title>
<!-- 设置网页图标 -->
<link rel="shortcut icon" href="../../images/favicon.png" type="image/x-icon" />
<!-- 引入CSS -->
<link rel="stylesheet" href="../../layui/css/layui.css">
<link rel="stylesheet" href="../css/mystyle.css">
<!-- 引入JS -->
<script src="../../layui/layui.js"></script>
<script type="text/javascript">
layui.use([ 'layer', 'table', 'form' ], function() {
	var $ = layui.$,layer = layui.layer,table = layui.table,form = layui.form,router = layui.router();
	/* 数据库ID */
	var dbid=router.search.dbid;
	/* 数据库模式名 */
	var schema=router.search.schema;
	/* 数据库表名 */
	var tablename=router.search.tablename;
	/* 加载模板 */
	$.ajax({
		type : "GET",
		url : "/templateset/templatesets?page=1&rows=500",
		async : true,
		dataType : "json",
		success : function(data) {
			/* 返回成功处理数据 */
			if (data.code == 200) {
				$.each(data.data.rows, function(idx, row) {
					$('select[name="templateSetId"]').append("<option value='" + row.id + "'>" + row.setName + "</option>");
				});
				/* 一定初始化一次不然事件会失效 */
				form.render();
			}
		}
	});
	/* 初始化数据表 */
	table.render({
		elem : '#table_fields',
		id:'layer_table_fields',
		toolbar : '#toolbar_generate',
		title : '表字段表格',
		size : 'lg',
		defaultToolbar : [],
		cols: [[
             {type:'numbers', width:100, title: '编号', sort: true},
             {field:'colName', width:300, title: '字段名', sort: true},
             {field:'colType', width:200, title: '字段类型',sort: true},
             {field:'isPk', width:120, title: '是否主键',align:'center',templet: function(row){
                if(row.isPk=="PRI"){
                	return '<i class="layui-icon" style="color:red;">&#xe605;</i>';
                }
                return '<i class="layui-icon" style="color:gray;">&#x1006;</i>';
             }},
             {field:'isNullable', width:120, title: '是否可为空',align:'center', sort: true},
             {field:'comments', title: '字段备注'},
             {field:'defaultValue', width:200, title: '默认值'}
	       	]],
		url : '/column/columns?schema='+schema+'&tableName='+tablename,
		headers: {dbid: dbid},
		height : 'full-5',
		page : true,
		limit : 20,
		limits : [10,20,30,40,60,80,100,200,500],
		even : true,
		request : {
			pageName : 'page',
			limitName : 'rows'
		},
		response : {
			statusCode : 200
		},
		parseData : function(res) {
			return {
				"code" : res.code,
				"msg" : res.msg,
				"count" : res.data.total,
				"data" : res.data.rows
			};
		}, 
		done: function(res, curr, count){
			/* 完成渲染查询后端是否存在本表配置 */
			$.ajax({
				type : "GET",
				url : "/table/config/config",
				data: { 'dbId': dbid, 'tableName': tablename },
				dataType : "json",
				success : function(data) {
					if (data.code == 200) {
					   /* 存在则填充 */
					   form.val('form_gen',data.data);
					}else{
					   /* 不存在给隐藏属性赋值 */
					   $('input[name="dbId"]').val(dbid);
					   $('input[name="tableName"]').val(tablename);
					}
				},
				error : function(xhr,status,error){
			        /*错误信息处理*/
			        var data = JSON.parse(xhr.responseText);
			        layer.msg(data.msg);
				}
			});
		},
		error : function(xhr,status,error){
	        /*错误信息处理*/
	        var data = JSON.parse(xhr.responseText);
	        layer.msg(data.msg);
		}
	});
	/* 保存配置 */
	form.on('submit(btn_generate_save)', function(data) {
		/* 调用接口 */
		$.ajax({
			type : "POST",
			url : "/table/config/config",
			data: JSON.stringify(data.field),
			contentType:"application/json",
			dataType : "json",
			success : function(data) {
				/* 处理数据 */
				if (data.code == 200) {
					layer.msg(data.msg,{icon:6});
				}else{
					layer.msg(data.msg);
				}
			},
			error : function(xhr,status,error){
		        /*错误信息处理*/
		        var data = JSON.parse(xhr.responseText);
		        layer.msg(data.msg);
			}
		});
		return false;
	});
	/* 生成代码 */
	form.on('submit(btn_generate_gen)', function(data) {
		/* 修改路径设置方法 */
		$("#form_gen").attr("action", "/table/config/generate").attr("method", "POST")
		layer.msg('下载成功,请前往浏览器下载查看文件！',{icon: 1});
		return true;
	});
	/* 返回按钮 */
	$(document).on('click', '#btn_seachform_back', function(){
		/* 返回 */
		window.history.back(-1);
	});
});
</script>
</head>
<script type="text/html" id="toolbar_generate">
  <div class="layui-fluid">
   <form id="form_gen" class="layui-form layui-form-pane layui-form-widen" lay-filter="form_gen">
   <input type="hidden" name="dbId">
   <input type="hidden" name="tableName">
   <div class="layui-row layui-col-space10">
    <div class="layui-col-md4">
      <label class="layui-form-label">请选择模板</label>
      <div class="layui-input-block">
        <select name="templateSetId" lay-filter="templateset_select">
          <option value="" selected>系统默认模板</option>
        </select>
      </div>
    </div>
    <div class="layui-col-md4">
      <label class="layui-form-label">作者名</label>
      <div class="layui-input-block">
        <input type="text" name="author" autocomplete="off" lay-verType="tips" lay-verify="required" placeholder="请输入作者名" class="layui-input" value="LiuGangQiang">
      </div>
    </div>
    <div class="layui-col-md3 layui-col-md-offset1" style="text-align: right;">
      <button type="submit" lay-submit class="layui-btn" lay-filter="btn_generate_save"><i class="layui-icon">&#xe66c;</i>&nbsp;保存配置</button>
      <button type="submit" lay-submit class="layui-btn layui-btn-normal" lay-filter="btn_generate_gen"><i class="layui-icon">&#xe609;</i>&nbsp;生成代码</button>
      <button type="button" class="layui-btn layui-btn-warm" id="btn_seachform_back"><i class="layui-icon">&#xe603;</i>&nbsp;返回</button>		
    </div>
   </div>
   <div class="layui-row layui-col-space10">
    <div class="layui-col-md4">
      <label class="layui-form-label">去掉表前缀</label>
      <div class="layui-input-block">
        <input type="text" name="prefix" autocomplete="off" lay-verType="tips" lay-verify="" placeholder="请输入需要去掉的表前缀" class="layui-input">
      </div>
    </div>
    <div class="layui-col-md4">
      <label class="layui-form-label">包名</label>
      <div class="layui-input-block">
        <input type="text" name="packageName" autocomplete="off" lay-verType="tips" lay-verify="required" placeholder="请输入包名" class="layui-input" value="com">
      </div>
    </div>
    <div class="layui-col-md4">
      <label class="layui-form-label">Entity名</label>
      <div class="layui-input-block">
        <input type="text" name="entityName" autocomplete="off" lay-verType="tips" lay-verify="required" placeholder="请输入Entity名" class="layui-input" value="%sPo">
      </div>
   </div>
   <div class="layui-row layui-col-space10">
    </div>
    <div class="layui-col-md4">
      <label class="layui-form-label">Mapper名</label>
      <div class="layui-input-block">
        <input type="text" name="mapperName" autocomplete="off" lay-verType="tips" lay-verify="required" placeholder="请输入Mapper名" class="layui-input" value="%sDao">
      </div>
    </div>
    <div class="layui-col-md4">
      <label class="layui-form-label">XML名</label>
      <div class="layui-input-block">
        <input type="text" name="xmlName" autocomplete="off" lay-verType="tips" lay-verify="required" placeholder="请输入XML名" class="layui-input" value="%sDao">
      </div>
    </div>
    <div class="layui-col-md4">
      <label class="layui-form-label">IService名</label>
      <div class="layui-input-block">
        <input type="text" name="serviceName" autocomplete="off" lay-verType="tips" lay-verify="required" placeholder="请输入IService名" class="layui-input" value="I%sService">
      </div>
    </div>
   </div>
   <div class="layui-row layui-col-space10">
    <div class="layui-col-md4">
      <label class="layui-form-label">Service名</label>
      <div class="layui-input-block">
        <input type="text" name="serviceImplName" autocomplete="off" lay-verType="tips" lay-verify="required" placeholder="请输入Service名" class="layui-input" value="%sService">
      </div>
    </div>
    <div class="layui-col-md4">
      <label class="layui-form-label">Controller名</label>
      <div class="layui-input-block">
        <input type="text" name="controllerName" autocomplete="off" lay-verType="tips" lay-verify="required" placeholder="请输入Controller名" class="layui-input" value="%sController">
      </div>
    </div>
    <div class="layui-col-md4">
      <label class="layui-form-label">Entity包名</label>
      <div class="layui-input-block">
        <input type="text" name="entityPackage" autocomplete="off" lay-verType="tips" lay-verify="required" placeholder="请输入Entity包名" class="layui-input" value="model.po">
      </div>
    </div>
   </div>
   <div class="layui-row layui-col-space10">
    <div class="layui-col-md4">
      <label class="layui-form-label">IService包名</label>
      <div class="layui-input-block">
        <input type="text" name="servicePackage" autocomplete="off" lay-verType="tips" lay-verify="required" placeholder="请输入IService包名" class="layui-input" value="service.iface">
      </div>
    </div>
    <div class="layui-col-md4">
      <label class="layui-form-label">Service包名</label>
      <div class="layui-input-block">
        <input type="text" name="serviceImplPackage" autocomplete="off" lay-verType="tips" lay-verify="required" placeholder="请输入Service包名" class="layui-input" value="service.impl">
      </div>
    </div>
    <div class="layui-col-md4">
      <label class="layui-form-label">Mapper包名</label>
      <div class="layui-input-block">
        <input type="text" name="mapperPackage" autocomplete="off" lay-verType="tips" lay-verify="required" placeholder="请输入Mapper包名" class="layui-input" value="dao">
      </div>
    </div>
   </div>
   <div class="layui-row layui-col-space10">
    <div class="layui-col-md4">
      <label class="layui-form-label">Controller包名</label>
      <div class="layui-input-block">
        <input type="text" name="controllerPackage" autocomplete="off" lay-verType="tips" lay-verify="required" placeholder="请输入Controller包名" class="layui-input" value="controller">
      </div>
    </div>
    <div class="layui-col-md4">
      <label class="layui-form-label">DTO包名</label>
      <div class="layui-input-block">
        <input type="text" name="dtoPackage" autocomplete="off" lay-verType="tips" lay-verify="required" placeholder="请输入DTO包名" class="layui-input" value="model.dto">
      </div>
    </div>
    <div class="layui-col-md4">
      <label class="layui-form-label">VO包名</label>
      <div class="layui-input-block">
        <input type="text" name="voPackage" autocomplete="off" lay-verType="tips" lay-verify="required" placeholder="请输入VO包名" class="layui-input" value="model.vo">
      </div>
    </div>
   </div>
   <div class="layui-row layui-col-space10">
    <div class="layui-col-md4">
      <label class="layui-form-label">Convert包名</label>
      <div class="layui-input-block">
        <input type="text" name="convertPackage" autocomplete="off" lay-verType="tips" lay-verify="required" placeholder="请输入Convert包名" class="layui-input" value="model.convert">
      </div>
    </div>
   </div>
   </form>
  </div>
</script>
<body>
 <table class="layui-hide" id="table_fields" lay-filter="table_fields"></table>
</body>
</html>